//
//  NetworkTools.swift
//  BusinessIOS
//
//  Created by Rain on 2018/12/26.
//  Copyright © 2018 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire

enum MethodType{
    case GET
    case POST
}

class NetworkTools: NSObject {

    class func GetDataByNSURLSession(_ dataS:Data,URLstr:String,finishCallBack:@escaping (_ result: AnyObject)->()){
        
        //(1）设置网络请求路径
        let url:NSURL = NSURL(string:URLstr)!//不需要传递参数
        
        //(2) 创建请求对象
        let request:NSMutableURLRequest = NSMutableURLRequest(url: url as URL) //默认为get请求
        request.timeoutInterval = 5.0 //设置请求超时为5秒
        request.httpMethod = "POST"  //设置请求方法
        request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
        //设置请求体
        
        //把拼接后的字符串转换为data，设置请求体
        
        request.httpBody = dataS
        let session = URLSession.shared
        //(3) 发送请求
        let task = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
            
            print(data ?? 9)
            
            print(response ?? 0)
            print(error ?? 9)
            
            var dict:[String:AnyObject]? = nil
            
            
            do {
                dict  = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.init(rawValue: 0)) as? NSDictionary as! [String : AnyObject]?
            } catch {
                
            }
            print("%@",dict ?? 0)
            
            finishCallBack(dict as AnyObject)
            
        })
        task.resume()
    }
    
}

